package com.helloae.chap09;

import java.awt.BorderLayout;
import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;

import javax.swing.JFrame;

import com.esri.arcgis.beans.map.MapBean;
import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.carto.esriSelectionResultEnum;
import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geodatabase.QueryFilter;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;

/**
 * 选择Population>10000的所有点并高亮，功能等同于"Select By Attributes"。<br>
 * 示例章节：9.3.1
 * @author zhushijie
 */
public class SelectFeatures {
	/**
	 * 查询并选中结果（即高亮显示）
	 * @return 高亮过的图层:FeatureLayer
	 * @throws UnknownHostException
	 * @throws IOException
	 */
	public FeatureLayer selectFeatures() throws UnknownHostException, IOException{
		//使用ShapefileWorkspaceFactory.openFromFile的方法创建一个Workspace
		ShapefileWorkspaceFactory pWF = new ShapefileWorkspaceFactory();
		String ecliseWorkspace = System.getenv("Eclipse3.6Workspace");
		IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace) pWF
		.openFromFile(ecliseWorkspace + "HelloAE" + File.separator
				+ "DataSet" + File.separator + "chap09", 0);
		//加载图层
		IFeatureClass pFeatureClass = pFeatureWorkspace
		.openFeatureClass("idcities");
		
		FeatureLayer pFeatureLayer = new FeatureLayer();
		pFeatureLayer.setFeatureClassByRef(pFeatureClass);
		QueryFilter pQueryFilter = new QueryFilter();
		//设置查询条件
		pQueryFilter.setWhereClause("Population > 10000");
		//查询并选中要素
		pFeatureLayer.selectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
		
		return pFeatureLayer;
	}

    public static void main(String[] args)throws Exception{
        EngineInitializer.initializeVisualBeans();
        AoInitialize ao = new AoInitialize();
        ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
        
        MapBean map = new MapBean();
        
        //测试本方法
        SelectFeatures obj = new SelectFeatures();
        map.addLayer(obj.selectFeatures(), 0);
        
        JFrame frame = new JFrame("Hello Arcobjects");
        frame.add(map, BorderLayout.CENTER);
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        
        //可视化界面不需要ao.shutdown();否则一运行就会退出程序
    }
}
